header {
package com.aote.expression.antlr;
}

class ObjectExpression extends Parser ;
{
	private static final byte GE=0;
	private static final byte GT=1;
	private static final byte LE=2;
	private static final byte LT=3;
	private static final byte EQ=4;
	private static final byte NE=5;
	
	private boolean compare(String str1, String str2, byte op) {
		int result = str1.compareTo(str2);
		switch(op) {
			case GE:
				return result >= 0; 
			case GT:
				return result > 0;
			case LE:
				return result <= 0;
			case LT:
				return result < 0;
			case EQ:
				return result == 0;
			case NE:
				return result != 0;
		}
		return false;
	}
}

expression
returns [String result=null]
	: expressionItem (ADD expressionItem)*
	;
	
expressionItem
returns [String result=null]
	: objectPathItem
	| STRING
	;
	
objectPathItem
returns [String result=null]
	: objectItem (DOT objectItem)*
	;

objectItem
returns [Object result=null]
	: ID (MLEFT condition MRIGHT)*
	;

condition
returns [boolean result=false]
{
	String e1, e2;
	byte op;
}
	: e1=expression (op=operator e2=expression {result=compare(e1, e2, op);}
		| b:BETWEEN{System.out.println(b.getText());} LEFT expression COMMA expression RIGHT)
		| objectItem
	;

operator
returns [byte result=0]
	: GR {result=GR;}
	| LT {result=LT;}
	| GE {result=GE;}
	| LE {result=LE;}
	| EQ {result=EQ;}
	| NE {result=NE;}
	;

class ObjectLexer extends Lexer; 
options {filter=true;}
{
}

DOT
	: '.'
	;
COMMA
	: ','
	;
LEFT
	: '('
	;
RIGHT
	: ')'
	;
MLEFT
	: '['
	;
MRIGHT
	: ']'
	;
GE
	: ">="
	;
GT
	: '>'
	;
LE
	: "<="
	;
LT
	: '<'
	;
NE
	: "!="
	;
EQ
	: '='
	;
BETWEEN
	: "between"
	;
ADD
	: '+'
	;
ID
	: ('a'..'z'|'A'..'Z')('a'..'z'|'A'..'Z'|'0'..'9'|'_')*
	;
STRING
	: '"' (~('#' | '"' | '.' | '\u0080'..'\ufffe') | '\u0080'..'\ufffe')+ '"'
	;
